﻿<html>
<head>
    <title>驰骋工作流</title>
    <link href="../Scripts/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../Scripts/bootstrap/js/jquery.min.js"></script>
    <script src="../Scripts/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
    <link href="../Scripts/bootstrap/bootstrap-paginator/css/bootstrapv3.css" rel="stylesheet"
        type="text/css" />
    <link href="../Scripts/bootstrap/DatePicker/bootstrap-datepicker.min.css" rel="stylesheet"
        type="text/css" />
    <script src="../Scripts/bootstrap/bootstrap-paginator/js/bootstrap-paginator.js"
        type="text/javascript"></script>
    <script src="../Scripts/bootstrap/DatePicker/bootstrap-datepicker.min.js" type="text/javascript"></script>
    <script src="../Scripts/bootstrap/DatePicker/bootstrap-datepicker.zh-CN.min.js" type="text/javascript"></script>
    <script src="../Scripts/QueryString.js" type="text/javascript"></script>
    <!-- 引用通用的js文件. -->
    <script type="text/javascript" src="../Scripts/config.js"></script>
    <script type="text/javascript" src="../Comm/Gener.js"></script>
    <style type="text/css">
        .keyWordInput
        {
            border-top-right-radius: 5px;
            border-top-left-radius: 5px;
            border-bottom-right-radius: 5px;
            border-bottom-left-radius: 5px;
            height: 30px;
        }
        .menuGroup
        {
            float: left;
        }
        .menuGroup a
        {
            margin-left:10px;
        }
        .menuGroupSelected
        {
            color: Blue; /* 文字颜色 */
            font-weight: bold;
            text-decoration: none;
            padding-bottom:5px;
            border-bottom: 3px solid Blue;
        }
        .searchpanellabel
        {
            font-size:13px;
            margin-left:5px;
            margin-right:5px;
        }
        #searchDiv
        {
            padding-top:5px;
            padding-bottom:5px;
        }
    </style>
    <script type="text/javascript" language="javascript">
        //扩展设置信息
        var mapExt = null;
        //查询条件集合
        var condtionsArry = [];
        //查询条件，按照条件字段长度排序后
        var condtionFieldsArry = [];
        //页面启动函数.
        $(function () {
            //重置页面内容
            ResetHtml();
            //实体参数.
            var ensName = GetQueryString("EnsName");
            mapExt = new Entity("BP.Sys.MapExt");
            mapExt.MyPK = "DtlImp_" + ensName;
            var i = mapExt.RetrieveFromDBSources();

            if (i == 0) {
                alert("没有配置导入数据相关参数。");
                return;
            }
            if (mapExt.Tag2 == "") {
                alert("参数配置错误，请联系管理员。");
                return;
            }

            //生成查询条件
            InitQueryParameters();
            //绑定事件
            $('.menuGroup a').each(function () {
                var aLabel = $(this);
                aLabel.click(function () {
                    $('.menuGroup a').each(function () {
                        $(this).removeClass("menuGroupSelected");
                    });
                    $(this).toggleClass("menuGroupSelected");
                });
            });

            //关键字输入提示
            var SearchTip = mapExt.GetPara("SearchTip");
            if (SearchTip == undefined) {
                SearchTip = "请输入关键字进行搜索";
            }

            $("#TB_Key").attr("placeholder", SearchTip);
            $('#TB_Key').bind('input propertychange', function () {
                InputKeyWordData();
            });
            //输入关键字查询数据
            InputKeyWordData();
            //设置表格内容高度
            SetTableSize();
        });

        //设置表格高度
        function SetTableSize() {
            var height = $(window).innerHeight();
            height = height - 100;
            if (condtionsArry && condtionsArry.length > 0) {
                //展现方式,0平铺、1下拉
                var selectType = mapExt.GetPara("SelectType");
                if (selectType == "0") {
                    height = height - 50;
                }
                if (condtionsArry.length >= 3) {
                    var TB_Key = $("#TB_Key");
                    TB_Key.css("width", "260px");
                    height = height - 40;
                }
                if (condtionsArry.length > 6) {
                    height = height - 40;
                }
            } else {
                $("#groupTr").remove();
            }
            //表格内容高度
            $("#tableContent").height(height);
        }
        
        //初始化查询条件
        function InitQueryParameters() {
            var ifcData = null;
            //$Para=Dept#Label=所在班级#ListURL=/DataUser/Handler.ashx?DoType=DtlImpReqAll
            var condtions = mapExt.Tag1;
            if (condtions == "")
                return;

            //组合查询条件数组
            var tempArry = condtions.split("$");
            $.each(tempArry, function (i, obj) {
                CondtionArryPush(obj);
            });

            //复制查询条件，排序
            condtionFieldsArry = jQuery.extend(true, [], condtionsArry);
            //利用js中的sort方法  
            condtionFieldsArry.sort(function (a, b) {
                return b.FieldKey.length - a.FieldKey.length;
            });
            //生成查询条件控件
            CreateCondtionCtrls();
        }

        //根据条件数组生成查询控件
        function CreateCondtionCtrls() {
            //生成查询条件控件
            var searchPanel = $("#searchDiv");
            //展现方式,0平铺、1下拉
            var selectType = mapExt.GetPara("SelectType");
            if (selectType == "1") {
                $("#groupTr").remove();
            }

            //平铺取第一个下拉框
            if (selectType == "0") {
                var groupItems = $.grep(condtionsArry, function (condtion) {
                    return condtion.CtrlType == "ListURL" || condtion.CtrlType == "ListSQL" || condtion.CtrlType == "EnumKey";
                });
                CreateGroupPanel(groupItems);
            }
            //关键字后面的查询条件
            var noFirstRow = false;
            $.each(condtionsArry, function (i, condtion) {
                //第一行条件个数
                var countRow = 3;
                //如果不是第一行，条件的个数
                if (noFirstRow == true) {
                    countRow = 4;
                }
                if (i !=0 && !(i % countRow)) {
                    searchPanel.append("<br/><br/>");
                    noFirstRow = true;
                }

                //排除已创建分组条件
                var groupField = $("#HD_GroupField").val();
                if (groupField != "" && condtion.FieldKey == groupField) {
                    return;
                }

                var label = $("<label>" + condtion.Label + "</label>");
                label.addClass("searchpanellabel");
                searchPanel.append(label);
                switch (condtion.CtrlType) {
                    case "ListURL":
                    case "ListSQL":
                        var dbTyep = condtion.CtrlType == "ListSQL" ? 0 : 1;
                        var select = $("<select></select>");
                        select.attr("id", condtion.FieldKey);
                        select.attr("name", condtion.FieldKey);
                        searchPanel.append(select);
                        var ifcData = DBAccess.RunDBSrc(condtion.Val, dbTyep);
                        if (ifcData && ifcData.length > 0) {
                            var oKeys = [];
                            for (var key in ifcData[0]) {
                                oKeys.push(key);
                            }
                            GenerBindDDL(condtion.FieldKey, ifcData, oKeys[0], oKeys[1], "");
                            $("#" + condtion.FieldKey).change(function () {
                                InputKeyWordData();
                            });
                        }
                        break;
                    case "EnumKey":
                        var select = $("<select></select>");
                        select.attr("id", condtion.FieldKey);
                        select.attr("name", condtion.FieldKey);
                        searchPanel.append(select);
                        GenerBindEnumKey(condtion.FieldKey, condtion.Val, "");
                        $("#" + condtion.FieldKey).change(function () {
                            InputKeyWordData();
                        });
                        break;
                    default:
                        var defVal = condtion.Val;
                        defVal = defVal.replace('<br/>', '');
                        defVal = defVal.replace('@Now', "(new Date())");
                        defVal = new Date(eval(defVal));
                        defVal = FormatDate(defVal, "yyyy-MM-dd");
                        var inputDate = $("<input type='text'/>");
                        inputDate.attr("id", condtion.FieldKey);
                        inputDate.attr("name", condtion.FieldKey);
                        inputDate.val(defVal);
                        searchPanel.append(inputDate);
                        $("#" + condtion.FieldKey).datepicker({
                            language: "zh-CN",
                            autoclose: true,
                            startView: 0,
                            format: "yyyy-mm-dd",
                            clearBtn: true,
                            todayBtn: false,
                            endDate: new Date()
                        }).on('changeDate', function (ev) {
                            InputKeyWordData();
                        });
                        return;
                }
            });
            //重新计算高度
            SetTableSize();
        }

        //创建分组面板
        function CreateGroupPanel(groupItems) {
            $("#groupDiv").html("");
            if (groupItems.length == 0) {
                return;
            }

            var groupItem = groupItems[0];
            var dbTyep = groupItem.CtrlType == "ListSQL" ? 0 : 1;
            var ifcData = DBAccess.RunDBSrc(groupItem.Val, dbTyep);
            $("#HD_GroupField").val(groupItem.FieldKey);
            $("#groupDiv").append("<a id='all' class='menuGroupSelected' href='javascript:void(0);'>全部</a>");
            var hd_GroupValue = $("<input type='hidden' value='' />");
            hd_GroupValue.attr("id",groupItem.FieldKey);
            $("#groupDiv").append(hd_GroupValue);

            if (ifcData && ifcData.length > 0) {
                var oKeys = [];
                var noKey = "";
                var nameKey = "";
                for (var key in ifcData[0]) {
                    oKeys.push(key);
                }

                noKey = oKeys[0];
                nameKey = oKeys[1];
                $.each(ifcData, function (i, dataObj) {
                    var no = dataObj[noKey];
                    var name = dataObj[nameKey];
                    $("#groupDiv").append("<a id='" + no + "' href='javascript:void(0);'>" + name + "</a>");
                });
            }
            $("#groupDiv a").unbind("click").bind("click", function () {
                var value = this.id;
                var groupField = $("#HD_GroupField").val();
                $("#" + groupField).val(value);
                InputKeyWordData();
            });
        }

        //添加查询条件到数组
        function CondtionArryPush(condtion) {
            if (condtion == "") {
                return;
            }
            var paras = condtion.split("#");
            if (paras.length != 3) {
                return;
            }

            var item = {};
            var paraKey = "", labelText = "";
            var ctrType = "Date", ctrValue = "";
            $.each(paras, function (j, para) {
                if (para.indexOf("Para=") > -1) {
                    paraKey = para.replace("Para=", "");
                }
                if (para.indexOf("Label=") > -1) {
                    labelText = para.replace("Label=", "");
                }
                if (para.indexOf("ListURL=") > -1) {
                    ctrValue = para.replace("ListURL=", "");
                    ctrType = "ListURL";
                }
                if (para.indexOf("ListSQL=") > -1) {
                    ctrValue = para.replace("ListSQL=", "");
                    ctrType = "ListSQL";
                }
                if (para.indexOf("EnumKey=") > -1) {
                    ctrValue = para.replace("EnumKey=", "");
                    ctrType = "EnumKey";
                }
                if (para.indexOf("DefVal=") > -1) {
                    ctrValue = para.replace("DefVal=", "");
                }
            });
            item.CtrlType = ctrType;
            item.FieldKey = paraKey;
            item.Label = labelText;
            item.Val = ctrValue;
            condtionsArry.push(item);
        }

        //输入关键字进行查询
        function InputKeyWordData() {
            var data = null;
            var UserNo = GetQueryString("UserNo");
            var RefPKVal = GetQueryString("RefPKVal");
            var keyWord = $("#TB_Key").val();
            var dbSrc = mapExt.Tag2;
            var reg = new RegExp("@Key", "g");
            dbSrc = dbSrc.replace(reg, keyWord);
            $.each(condtionFieldsArry, function (i, obj) {
                var regC = new RegExp("@" + obj.FieldKey, "g");
                var value = $("#" + obj.FieldKey).val();
                if (value == null || value == "null") value = "";
                dbSrc = dbSrc.replace(regC, value);
            });
            data = DBAccess.RunDBSrc(dbSrc, mapExt.DBType);

            $("#jsonDataTable").html("");
            if (data && data.length > 0) {
                InitTableByData(data);
            }
        }

        //数据返回Table
        function InitTableByData(dataTable) {
            //获取数据列名
            var columns = [];
            var texts = [];
            var heads = [];
            if (mapExt.Tag.length > 0) {
                texts = mapExt.Tag.split(',');
            }
            for (var col in dataTable[0]) {
                columns.push(col);
            }
            
            //以中文个数为表格显示列的个数
            if (texts && texts.length > 0) {
                $.each(texts, function (i, column) {
                    if (column == "")
                        return;
                    var columns = column.split("=");
                    if (columns.length == 2) {
                        var column = {};
                        column.No = columns[0];
                        column.Name = columns[1];
                        heads.push(column);
                    }
                });
            } else {
                $.each(columns, function (i, col) {
                    var column = {};
                    column.No = col;
                    column.Name = col;
                    heads.push(column);
                });
            }

            //表头
            var thread = $('<thead></thead>');
            var threadTr = $('<tr></tr>');
            //复选框
            var thFirst = $("<th style='width:35px;'></th>");
            var ckBox = $("<input type='checkbox' onclick='CheckAll()' id='CB_CheckAll' >");
            thFirst.append(ckBox);
            threadTr.append(thFirst);
            $.each(heads, function (i, head) {
                var th = $("<th></th>");
                th.text(head.Name);
                th.data(head);
                threadTr.append(th);
            });
            thread.append(threadTr);
            $("#jsonDataTable").append(thread);

            //生成表格体
            var tbody = $('<tbody></tbody>');
            $.each(dataTable, function (i, dataObj) {
                var tbodyTr = $('<tr></tr>');
                var tdFirst = $("<td></td>");
                //第一列为主键
                var refPK = heads[0].No;
                var no = dataObj[refPK];

                var ckBox = $("<input type='checkbox'/>");
                ckBox.attr("id", "CB_" + no);
                ckBox.data(dataObj);
                tdFirst.append(ckBox);

                tbodyTr.append(tdFirst);
                //字段列
                $.each(heads, function (i, head) {
                    var td = $("<td></td>");
                    var text = "";

                    if (head.No && head.No != "") {
                        text = dataObj[head.No];
                    }
                    td.text(text);
                    tbodyTr.append(td);
                });
                tbody.append(tbodyTr);
            });
            $("#jsonDataTable").append(tbody);
        }

        //ajax获取后台数据
        function initTable() {
            var tbody = "";
            $.ajax({
                type: 'POST',
                dataType: "json",
                async: false,
                url: "table.jsp", //请求的action路径页面
                data: { "flag": true },
                error: function () {//请求失败处理函数 
                    alert('请求失败');
                },
                success: function (data) { //请求成功后处理函数。 
                    tbody = "<tr class='success'><th>设备标识号</td><th  align='center'>设备类别</td><th  align='center'>参数编码</td><th  align='center'>失效模式编码</td><th  align='center'>数据来源</td></tr>";
                    $.each(data.list, function (i, n) {
                        var trs = "";
                        trs += "<tr><td align='center'>" + n.sbbsh + "</td><td align='center'>" + n.sblb + "</td><td align='center'>" + n.csbm + "</td><td align='center'>" + n.sxcode + "</td><td align='center'>" + n.sjly + "</td></tr>";
                        tbody += trs;
                    });
                    $("#testTable").html(tbody);
                    var pageCount = data.pageCount; //取到pageCount的值
                    var currentPage = data.CurrentPage; //得到currentPage

                    var options = {
                        bootstrapMajorVersion: 3, //版本
                        currentPage: currentPage, //当前页数
                        totalPages: pageCount, //总页数
                        numberOfPages: 5,
                        itemTexts: function (type, page, current) {
                            switch (type) {
                                case "first":
                                    return "首页";
                                case "prev":
                                    return "上一页";
                                case "next":
                                    return "下一页";
                                case "last":
                                    return "末页";
                                case "page":
                                    return page;
                            }
                        }, //点击事件，用于通过Ajax来刷新整个list列表
                        onPageClicked: function (event, originalEvent, type, page) {
                            $.ajax({
                                url: "table.jsp",
                                type: "Post",
                                dataType: "json",
                                data: "page=" + page,
                                success: function (data) {
                                    tbody1 = "<tr class='success'><th>设备标识号</td><th  align='center'>设备类别</td><th  align='center'>参数编码</td><th  align='center'>失效模式编码</td><th  align='center'>数据来源</td></tr>";
                                    $.each(data.list, function (i, n) {
                                        var trs = "";
                                        trs += "<tr><td align='center'>" + n.sbbsh + "</td><td align='center'>" + n.sblb + "</td><td align='center'>" + n.csbm + "</td><td align='center'>" + n.sxcode + "</td><td align='center'>" + n.sjly + "</td></tr>";
                                        tbody1 += trs;
                                    });
                                    $("#testTable").html(tbody1);
                                }
                            });
                        }
                    };
                    $('#example').bootstrapPaginator(options);
                }
            });
        }

        //重置标签
        function ResetHtml() {
            $("#groupDiv").html("");
            $("#jsonDataTable").html("");
        }

        //全选
        function CheckAll() {
            var checked = document.getElementById('CB_CheckAll').checked;
            $.each($(":checkbox"), function () {
                this.checked = checked;
            });
        }

        //确定按钮，导入数据
        function DalogModelOK() {
            //实体参数.
            var EnsName = GetQueryString("EnsName");
            var refPKVal = GetQueryString("RefPKVal");
            var PWorkID = GetQueryString("PWorkID");
            var FK_Node = GetQueryString("FK_Node");
            var FID = GetQueryString("FID");
            var UserNo = GetQueryString("UserNo");
            var paraDatas = [];
            $.each($(":checkbox"), function (i, cb) {
                if (cb.checked) {
                    var data = $(cb).data();
                    if (data) {
                        paraDatas.push(data);
                    }
                }
            });
            if (paraDatas.length == 0) {
                alert("没有选择导入数据，请选择！");
                return;
            }
            //插入明细表数据
            var dtl = new Entity(EnsName);
            $.each(paraDatas, function (i, data) {
                dtl.CopyJSON(data);
                dtl.RefPK = refPKVal;
                dtl.Rec = UserNo;
                dtl.FID = FID;
                dtl.Insert();
            });

            //执行完毕关闭弹窗
            DalogModelCose();
        }

        //取消，关闭弹出层
        function DalogModelCose() {
            if (window.parent && window.parent.CloseBootstrapDialog) {
                window.parent.CloseBootstrapDialog("取消");
            }
        }
    </script>
</head>
<body>
    <form id="cc">
    <table style="border: 0px; width: 99%;table-layout: fixed;">
        <tr>
            <td>
                <div id="searchDiv" style='text-align: left;'>
                    <input type="text" id="TB_Key" value="" class="keyWordInput" style="width: 50%;" placeholder="请输入关键字进行搜索"/>
                </div>
            </td>
        </tr>
        <tr id="groupTr">
            <td style="height: 60px; text-align: left;">
                <div id="groupDiv" class="menuGroup">
                    <a href="javascript:void(0);">全部</a> 
                    <a class="menuGroupSelected" href="javascript:void(0);">携程商旅</a> 
                    <a href="javascript:void(0);">滴滴出行</a>
                </div>
            </td>
        </tr>
        <tr>
            <td style="border: 1px #FF8000 solid;">
                <div id="tableContent" style="overflow:auto;">
                    <div>
                        <table class="table table-striped" id='jsonDataTable'></table>
                    </div>
                    <div style="text-align: left;">
                        <ul id="examplePagetions"></ul>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td style="float:right;">
                <div style="margin-top:10px; margin-right:10px;">
                    <input type="button" class="btn" onclick="DalogModelCose()" value="取消" />
                    <input type="button" class="btn btn-danger" onclick="DalogModelOK()" value="确定" />
                </div>
            </td>
        </tr>
    </table>
    </form>
    <input type="hidden" id="HD_GroupField" value="" />
</body>
</html>
